package com.qiwenshare.file.component;

import com.qiwenshare.common.exception.QiwenException;
import com.qiwenshare.file.api.IDepartmentService;
import com.qiwenshare.file.api.IUserService;
import com.qiwenshare.file.api.JobService;
import com.qiwenshare.file.api.RoleService;
import com.qiwenshare.file.domain.Department;
import com.qiwenshare.file.domain.Job;
import com.qiwenshare.file.domain.SysRole;
import com.qiwenshare.file.domain.user.Role;
import com.qiwenshare.file.domain.user.UserBean;
import com.qiwenshare.file.office.entities.User;
import com.qiwenshare.file.service.JobServiceImpl;
import com.qiwenshare.file.service.UserService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author MJ
 * @title qiwen-file
 * @date 2023/9/19 14:29
 * @description TODO
 */
@Configuration
@Slf4j
public class InitData implements InitializingBean {

    private final IUserService usersService;

    private final IDepartmentService departmentService;
    private final JobService jobService;

    private final RoleService roleService;

    /**
     * 存放sysUserId与员工的映射关系
     */
    @Getter
    private static Map<String, String> userStaffMap = new ConcurrentHashMap<>();
    private static Map<String, String> departmentMap = new ConcurrentHashMap<>();

    private static Map<String, String> jobMap = new ConcurrentHashMap<>();
    private static Map<Long, String> roleMap = new ConcurrentHashMap<>();

    public InitData(IUserService u, IDepartmentService d, JobService jobService, RoleService roleService) {
        this.usersService = u;
        this.departmentService = d;
        this.jobService = jobService;
        this.roleService = roleService;
    }

    @Override
    public void afterPropertiesSet() {
        log.info("初始化系统用户与员工名称映射关系...");
        initUserStaffMap();
        initDepartmentMap();
        initJobMap();
        initRoleMap();
    }

    /**
     * 手动刷新系统初始化数据
     */
    public void manualInitData() {
        log.info("初始化系统用户与员工名称映射关系...");
        initUserStaffMap();
        initDepartmentMap();
        initJobMap();
        initRoleMap();
        log.info("手动初始完成!!");
    }

    private void initUserStaffMap(){
        List<UserBean> sysUsers = usersService.getAllUser();
        userStaffMap = sysUsers.stream()
                .collect(Collectors.toConcurrentMap(
                        UserBean::getUserId,
                        UserBean::getUsername
                        ));
    }

    private void initDepartmentMap(){
        List<Department> departmentList = departmentService.getList();
        departmentMap = departmentList.stream()
                .collect(Collectors.toConcurrentMap(
                        Department::getDepartmentId,
                        Department::getDepartmentName
                ));
    }

    private void initJobMap(){
        List<Job> jobList = jobService.selectList();
        jobMap = jobList.stream()
                .collect(Collectors.toConcurrentMap(
                        Job::getJobId,
                        Job::getJobName
                ));
    }

    private void initRoleMap(){
        List<SysRole> roleList = roleService.queryAllRoleList();
        roleMap = roleList.stream()
                .collect(Collectors.toConcurrentMap(
                        SysRole::getRoleId,
                        SysRole::getRoleName
                ));
    }

    /**
     * 通过用户id获取员工名字
     * @param userId 用户id
     * @return 员工名字
     */
    public static String getStaffNameByUserId(String userId){
        if (userId == null) {
            return null;
        }
        String staffName = null;
        for (String key: userStaffMap.keySet()) {
            if (userId.equals(key)) {
                staffName = userStaffMap.get(userId);
                break;
            }
        }

        return staffName;
    }

    public static String getDepartmentNameById(String departmentId){
        if (departmentId == null) {
            return null;
        }
        String departmentName = null;
        for (String key: departmentMap.keySet()) {
            if (departmentId.equals(key)) {
                departmentName = departmentMap.get(departmentId);
                break;
            }
        }
        return departmentName;
    }

    public static String getJobNameById(String jobId){
        if (jobId == null) {
            return null;
        }
        String jobName = null;
        for (String key: jobMap.keySet()) {
            if (jobId.equals(key)) {
                jobName = jobMap.get(jobId);
                break;
            }
        }
        return jobName;
    }
    public static String getRoleNameById(Long roleId){
        if (roleId == null) {
            return null;
        }
        String roleName = null;
        for (Long key: roleMap.keySet()) {
            if (roleId.equals(key)) {
                roleName = roleMap.get(roleId);
                break;
            }
        }
        return roleName;
    }
}
